package com.orhon.smartcampus.modules.material.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.material.dao.FloorDao;
import com.orhon.smartcampus.modules.material.entity.Floor;
import com.orhon.smartcampus.modules.material.entity.Unit;
import com.orhon.smartcampus.modules.material.gqlModel.GQLFloor;
import com.orhon.smartcampus.modules.material.gqlModel.GQLUnit;
import com.orhon.smartcampus.modules.material.mm.FloorMapper;
import com.orhon.smartcampus.modules.material.mm.UnitMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
@Component
public class FloorFetcher  implements DataFetcher{
	
	@Autowired
	FloorDao floorDao;


	@Autowired
	TokenService tokenService;


	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from Floor where deleted_at = null and id = "+id+" and school_id = " + schoolId;
		Floor item =  floorDao.findId(hql, null);
		if(item!=null) {
			GQLSchool GgqlSchool = null;
			if (env.getSelectionSet().contains("school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				GgqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}
			
			GQLUnit gqlUnit=null;
			if (env.getSelectionSet().contains("unit")){
				Unit unit = item.getMaterialUnitByUnitId();
				gqlUnit = UnitMapper.MAPPER.toUnit(unit);
			}

			GQLFloor floor = FloorMapper.MAPPER.toFloor(item);
			floor.setSchool(GgqlSchool);
			floor.setUnit(gqlUnit);
			return floor;
		}
		return null;
	}
}
